package circle;

/**
 * @author 陈艺博
 * @date 2021-02-25 12:00
 * @description 循环双端队列
 **/
public class CircleDeque<E> {

    private int front;
    private int size;
    private E[] elements;
    private static final int DEFAULT_CAPACITY = 10;

    public CircleDeque() {
        elements = (E[]) new Object[DEFAULT_CAPACITY];
    }
    public void clear(){
        for (int i = 0; i < size; i++) {
            elements[index(i)]= null;
        }
        front = 0;
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 队尾添加元素
     *
     * @param element
     */
    public void enQueueRear(E element) {
        // 动态扩容
        ensureCapacity(size + 1);
        elements[index(size)] = element;
        size++;
    }

    /**
     * 队头添加元素
     *
     * @param element
     */
    public void enQueueFront(E element) {
        ensureCapacity(size + 1);
        front = index(-1);
        elements[front] = element;
        size++;
    }

    /**
     * 对头出队
     *
     * @return
     */
    public E deQueueFront() {
        E frontElem = elements[front];
        elements[front] = null;
        front = index(1);
        size--;
        return frontElem;
    }

    /**
     * 队尾出队
     *
     * @return
     */
    public E deQueueRear() {
        int rearIndex = index(size - 1);
        E rear = elements[rearIndex];
        elements[rearIndex] = null;
        size--;
        return rear;
    }

    /**
     * 获取队列的头元素
     *
     * @return
     */
    public E front() {
        return elements[front];
    }

    /**
     * 获取队列的尾元素
     *
     * @return
     */
    public E    rear() {
        return elements[index(size - 1)];
    }

    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) {
            return;
        }
        //新容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[index(i)];
        }
        elements = newElements;
        front = 0;
    }

    /**
     * 将逻辑索引index, 装换为队列的真实索引
     *
     * @param index 逻辑索引
     * @return 队列对应的真实索引
     */
    private int index(int index) {
        index += front;
        if (index < 0) {
            return index + elements.length;
        }
        return index % elements.length;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("Capacity = ").append(elements.length)
                .append(", Size = ").append(size)
                .append(", Front = ").append(front)
                .append(", Element = [");
        for (int i = 0; i < elements.length; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append(elements[i]);
        }
        string.append(" ]");
        return string.toString();
    }
}
